package com.tencent.wxcloudrun.service.impl;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

import com.tencent.wxcloudrun.dto.UserCardExportRecordResDTO;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencent.wxcloudrun.domain.TUserCardApply;
import com.tencent.wxcloudrun.domain.TUserCardRecord;
import com.tencent.wxcloudrun.mapper.TUserCardRecordMapper;
import com.tencent.wxcloudrun.service.TUserCardRecordService;

/**
 * @author ziyin.zhao
 * @description 针对表【t_user_card_record(用户卡片记录表)】的数据库操作Service实现
 * @createDate 2025-04-22 16:43:56
 */
@Service
public class TUserCardRecordServiceImpl extends ServiceImpl<TUserCardRecordMapper, TUserCardRecord>
    implements TUserCardRecordService {

    @Override
    public void saveCardRecord(TUserCardApply apply) {
        TUserCardRecord record = new TUserCardRecord();
        record.setUserId(apply.getUserId());
        record.setCardId(apply.getCardId());
        record.setCount(apply.getCount());
        record.setMemo("申请审核通过");
        record.setType(1);
        record.setCreatedBy(String.valueOf(apply.getUserId()));
        record.setUpdatedBy(String.valueOf(apply.getUserId()));
        record.setCreatedTime(LocalDateTime.now());
        record.setUpdatedTime(LocalDateTime.now());
        this.save(record);
    }

    @Override
    public List<TUserCardRecord> queryCardRecord(Long userId, Long cardId) {
        LambdaQueryWrapper<TUserCardRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TUserCardRecord::getUserId, userId);
        queryWrapper.eq(TUserCardRecord::getCardId, cardId);
        queryWrapper.eq(TUserCardRecord::getEnableFlag, 1);
        queryWrapper.orderByDesc(TUserCardRecord::getUpdatedTime);
        return this.list(queryWrapper);
    }

    @Override
    public List<UserCardExportRecordResDTO> listUseRecord(Long actiId) {
        return this.baseMapper.listUseRecord(actiId);
    }
}
